const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();

var startPoint
Page({
  data: {
    globalStype:'',
    isFixedTop:false,
    tabbar:[],
    cloutGoodsPagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
    malllists:[
    
    ],
    shopId:'',
    mallindex:0,
    clickMe:true,
    buttonRight:0,
    buttonBottom:0,
    cartNum:"",
    lastPage:false,
    name:"",
    title:'点单',
    type:'',
    vip:'',
    openShareholder:'',
    openAgent:'',
    labelId:'',
    couponAccountId:'',
    navBarHeight:'',

  },

   //以下是按钮拖动事件
  buttonStart: function (e) {
    startPoint = e.touches[0]//获取拖动开始点
  },
  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1]//获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint//重置开始位置
    var buttonBottom = this.data.buttonBottom - translateY
    var buttonRight = this.data.buttonRight - translateX
    //判断是移动否超出屏幕
    if (buttonRight+50 >= this.data.windowWidth){
      buttonRight = this.data.windowWidth-50;
    }
    if (buttonRight<=0){
      buttonRight=0;
    }
    if (buttonBottom<=0){
      buttonBottom=0
    }
    if (buttonBottom + 50 >= this.data.windowHeight){
      buttonBottom = this.data.windowHeight-50;
    }
    this.setData({
      buttonBottom: buttonBottom,
      buttonRight: buttonRight
    })
  },
  buttonEnd: function (e) {
  },

  //获取购物车数量
  getCartNum(){
    Controllers.cartList({shopId:this.data.shopId}).then(res=>{
      let allNum=0
      res.data.normal.forEach(item=>{
        allNum+=item.num
      })
      this.setData({
        cartNum:allNum
      })
    })
  },

  //跳转购物车
  toCart(){
    wx.navigateTo({
      url: '/pages/cart/index/index?couponAccountId=' + this.data.couponAccountId,
    })
  },


  onLoad(option) {
    if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId,
      })
    }
    if(option.couponAccountId){
      this.setData({
        couponAccountId:option.couponAccountId
      })
    }
    // 获取标签
    this.selectLabel()
    console.log(option.type);
    let type=option.type
    this.setData({
      type
    })
    if(type=="vip"){
      this.setData({
        title:'vip专享',
        vip:1
      })
    }else if(type=="shareholder"){
      this.setData({
        title:'股东专享',
        openShareholder:1
      })
    }else if(type=="agent"){
      this.setData({
        title:'代理商专享',
        openAgent:1
      })
    }
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
    
    let that=this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          buttonBottom:250,//这里定义按钮的初始位置
          buttonRight:0,//这里定义按钮的初始位置
          navBarHeight :res.statusBarHeight+48 //导航栏高度
        })
      }
    })

  },
  onShow(){
    this.cloutGoodsPage()

    this.getCartNum()
  },
  tabChange(e){
    var index=e.detail.index;
    if(index==0){
      wx.reLaunch({
        url: "/pages/Index/Index"
      });
    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/TradingArea/TradingArea"
      });
    }
    if(index==3){
      wx.reLaunch({
        url: "/pages/couponBagCenter/couponBagCenter"
      });
    }
    if(index==4){
      wx.reLaunch({
        url: "/pages/my/my-index/my-index"
      });
    }
  },
  // 获取滚动条当前位置
  // onPageScroll: function (e) { 
  //   console.log(e.scrollTop)
  //   let query = wx.createSelectorQuery()
  //   query.select('#malllists').boundingClientRect((rect) => {
  //     if(rect){
  //       let top = rect.top;
  //       if (top < e.scrollTop - 64) {
  //         this.setData({
  //           isFixedTop: true
  //         })
  //       } else {
  //         this.setData({
  //           isFixedTop: false
  //         })
  //       }
  //     }
  //   }).exec()
  // },
  selectLabel(){
    Controllers.selectLabel({type:1,shopId:this.data.shopId}).then(res => {
      let list=[ {
        name:"全部",
        id:""
      }]
      this.setData({
        malllists:list.concat(res.data)
      })
      this.setData({
        labelId:this.data.malllists[0].id
      })
      this.cloutGoodsPage()
    });
  },

  getSearchText(e){
    this.setData({
      name:e.detail.value
    })
  },

  cloutGoodsPage(){
      this.setData({
        pageNumber:1
      })
      Controllers.labelGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:1,
        shopId:this.data.shopId,
        labelId:this.data.labelId,
        name:this.data.name,
        labelGoodsPage:0,
        vip:this.data.vip, 
        openAgent :this.data.openAgent, 
        openShareholder:this.data.openShareholder ,
        openActivity:0//不展示积分商城商品
      }).then(res => {
        if(res.data.list.length>0){
            for(let item of res.data.list){
              if(item.label){
                item.label=item.label.split('/')
              }
            }
        }
        this.setData({
          cloutGoodsPagedata:res.data.list,
          lastPage:res.data.lastPage
        })
      }).catch(res=>{
        this.setData({isloading:false})
      })
  },

  getMore(){
    if(this.data.lastPage==false){
      let pageNumber=this.data.pageNumber+1
      this.setData({
        pageNumber:pageNumber
      })
      Controllers.labelGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:pageNumber,
        shopId:this.data.shopId,
        labelId:this.data.labelId,
        name:this.data.name,
        openActivity:0
      }).then(res => {
        if(res.data.list.length>0){
            for(let item of res.data.list){
              if(item.label){
                item.label=item.label.split('/')
              }
            }
            let list=this.data.cloutGoodsPagedata
            let list2=res.data.list
            let list3=list.concat(list2)
          this.setData({
            cloutGoodsPagedata:list3,
            lastPage:res.data.lastPage
          })
        }
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }
  },
  
  onReachBottom() {
    this.getMore()
  },
  changemall(e){
    this.setData({
      cloutGoodsPagedata:[],
      pageNumber:1,
      pageSize:10,
      isEnd:false,
      isloading:true,
      mallindex:e.currentTarget.dataset.index,
      shopId:this.data.shopId,
      labelId:e.currentTarget.dataset.id
    })
    this.cloutGoodsPage(e.currentTarget.dataset.id)
  },
  //获取通知权限
  getNotice(){

    wx.navigateTo({
      url:"/pages/Mall/Coupon"
    })

    // wx.requestSubscribeMessage({
    //   tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','DlEG_b9R6f2bWjrW-jEmsoy_hcSHMMB9TPhh9q9GbCo'],
    //   success:res=>{
    //     console.log(res)
    //   },
    //   complete:()=>{
    //     wx.navigateTo({
    //       url:"/pages/Mall/Coupon"
    //     })
    //   }
    // })
  },


  toClickMe(){
    wx.navigateTo({
      url: '/pages/clickMe/clickMe',
    })
  },

  cancleClickMe(){
    this.setData({
      clickMe:false
    })
  },

  //加入购物车
  addCart(e){
    let value={}
    let index=e.currentTarget.dataset.index
    let list=this.data.cloutGoodsPagedata
    value.num=1
    value.shopId=this.data.shopId
    value.goodsId=e.currentTarget.dataset.id
    Controllers.addGoods(value).then(res=>{
      list[index].cartNum+=1
      this.setData({
        cloutGoodsPagedata:list
      })
      this.getCartNum()
    })
  },

  //清除购物车
  reduceCart(e){
    let value={}
    let index=e.currentTarget.dataset.index
    let list=this.data.cloutGoodsPagedata
    list[index].cartNum-=1
    value.cartId=e.currentTarget.dataset.cartid
    if(list[index].cartNum<=0){
      value.shopId=this.data.shopId
      value.cartIds=e.currentTarget.dataset.cartid
      Controllers.delGoods(value).then(res=>{
        this.setData({
          cloutGoodsPagedata:list
        })
        this.getCartNum()
      })
    }else{
      value.num=list[index].cartNum
      Controllers.updateCart(value).then(res=>{
        this.setData({
          cloutGoodsPagedata:list
        })
        this.getCartNum()
      })
    }
  }
})